import asyncio
import pathlib
import ssl
import websockets


user1_msp_dir = "/mnt/e/fabricdev/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/"
user2_msp_dir = "/mnt/e/fabricdev/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User2@org1.example.com/msp/"


async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)

ssl_context.load_cert_chain(
    certfile=f"{user1_msp_dir}/signcerts/cert.pem", keyfile=f"{user1_msp_dir}/keystore/User1.key")

ssl_context.load_verify_locations(f"{user2_msp_dir}/signcerts/cert.pem")
ssl_context.verify_mode = ssl.CERT_REQUIRED


start_server = websockets.serve(
    hello, "localhost", 1234, ssl=ssl_context
)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
